package com.whirl.org.controller;

import com.whirl.basic.utils.AjaxResult;
import com.whirl.basic.utils.PageList;
import com.whirl.org.entity.Employee;
import com.whirl.org.query.EmployeeQuery;
import com.whirl.org.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaowei
 * @date 2022/6/13 12:24
 */

@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    // 根据id删除员工
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        try {
            employeeService.deleteById(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    // 批量删除员工
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }


    // 根据id查询员工
    @GetMapping("/{id}")
    public Employee selectById(@PathVariable Long id) {
        return employeeService.selectById(id);
    }

    // 查询所有员工
    @GetMapping
    public List<Employee> selectAll() {
        return employeeService.selectAll();
    }

    /**
     * 新增或者更新员工  根据id判断
     * RequestBody 可以接收json格式的数据
     *
     * @param employee
     */
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                System.out.println("新增员工");
                employeeService.insert(employee);
            } else {
                System.out.println("更新员工");
                employeeService.update(employee);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    // 查询员工分页
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }
}
